package com.chang.controller;

import com.chang.BaseController;
import com.jfinal.core.ActionKey;
import com.mongodb.BasicDBObject;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import com.chang.common.DataBase;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class Book extends BaseController {
    @ActionKey("/ScoreSource")
    public void ToScoreSource(){
         ReturnPage("/jsp/book.jsp", "");
    }
    @ActionKey("/ScoreSource/IndexPage")
    public void ToIndex(){
         ReturnPage("/jsp/admin-main.jsp", "");
    }
    /**
     * 书籍列表
     */
    @ActionKey("/ScoreSource/table")
    public void ScoreSourceRaderList() throws UnsupportedEncodingException {
            BasicDBObject SortObject = new BasicDBObject();
            SortObject.put("createday",1);//排序，1为正序，-1为倒序
            String key = getPara("key");
            BasicDBObject FilterObject=null;
            if(StringUtils.isNotEmpty(key)){
                key = key.trim();
                if (key.equals(new String(key.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                    key = new String(key.getBytes("ISO-8859-1"), "utf-8");
                }
                //定义KEY模糊查询
                Pattern pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
                FilterObject = new BasicDBObject().append("$or",
                        new BasicDBObject[]{
                                new BasicDBObject("score_source_code", pattern)
                        });
            }
            List<Document> DocmentList = DataBase.find("book",FilterObject,SortObject);
            if(DocmentList!=null && DocmentList.size()>0)
            {
                this.ReturnJSON(DocmentList);
            }
            else
            {
                this.ReturnJSON("");
            }
    }

    /**
     * 书籍添加
     */
    @ActionKey("/ScoreSource/Add")
    public void ScoreSourceCommitAdd()
    {
        Document CommitObject = FormToDocument(getRequest(),getParaNames());
        CommitObject.append("_id",DataBase.getSequence("book"));
        CommitObject.append("createday", new Date());
        DataBase.InsertNotAutoID("book", CommitObject);
        ReturnJSON("0","",null);
    }

    /**
     * 根据id查询书籍
     */
    @ActionKey("/ScoreSource/ReaderOne")
    public void ReaderOne()
    {
        String _id = getPara("_id");
        BasicDBObject FilterObject = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(_id))
                });
        List<Document> DocmentList =DataBase.find("book", FilterObject,null);
        ReturnJSON("0","",DocmentList);
    }

    /**
     * 编辑书籍
     */
    @ActionKey("ScoreSource/CommitEdit")
    public void update()
    {
        Document CommitObject = FormToDocument(getRequest(),getParaNames());
        BasicDBObject UpdateFilter = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(CommitObject.getString("_id")))

                });
        List<Document> checkList=DataBase.find("book", UpdateFilter, null);
        if(checkList!=null && checkList.size()>0)
        {
            CommitObject.remove("_id");
            DataBase.updateByFilter("book", UpdateFilter, CommitObject);
            ReturnJSON("0","",null);
        }
        else
        {
            ReturnJSON("-1","找不到该书籍！请确认该书籍是否存在！",null);
        }
    }

    @ActionKey("ScoreSource/Remove")
    public void delete(){
        String _id = getPara("_id");
        BasicDBObject FilterObject = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(_id))
                });
        DataBase.deleteByFilter("book", FilterObject);
        ReturnJSON("0","",null);
    }

}
